package ydzhao.weixin.tuisong.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ydzhao.weixin.tuisong.common.UserCom;

import java.util.HashMap;
import java.util.Map;

public class Tianqi {
    private static String ak = "nNvwZ8sk112mhpsFZFGPUKU8c7WEb0zk";
    //由于仅二人使用，使用当地`行政区编码`即可了，可以获取当地IP信息，天气，地区等等；
    private static String district_id1 = "530500";   //宝山
    private static String district_id2 = "530100";   //昆明
    private static String district_id3 = "320106";   //鼓楼
    private static String url = "";

    public static Map<String, Object> getNanjiTianqi(String openId) {
        Map<String,Object> map = new HashMap<>();
        String result = null;
        JSONObject today = new JSONObject();
        try {
            switch (openId){
                case UserCom.OPENID_YOU:
                    url = "https://api.map.baidu.com/weather/v1/?district_id=" + district_id1 + "&data_type=all&ak=" + ak;
                    break;
                case UserCom.OPENID_ME:
                    url = "https://api.map.baidu.com/weather/v1/?district_id=" + district_id3 + "&data_type=all&ak=" + ak;
                    break;
            }
            result = HttpUtil.getUrl(url);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("message").equals("success")) {
                JSONArray arr = jsonObject.getJSONObject("result").getJSONArray("forecasts");
                JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
                today = arr.getJSONObject(0);
                map.put("location",location);
                map.put("today",today);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static void main(String[] args) {
        System.out.println(getNanjiTianqi(UserCom.OPENID_ME));
    }
}
